import viz
import time
import win32gui
from win32com.shell import shell, shellcon

class Gui:
	
	def __init__(self, vizact, game):
		self.vizact = vizact
		self.game = game
		
		self.newGameButton = viz.addButton()
		self.newGameButton.setPosition(.05,.95) #Set its position.
		self.newGameButton.setScale(1,1) #Scale it.
		
		self.newGameText = viz.addText("New Game", viz.SCREEN)
		self.newGameText.setPosition(0.07,0.94)
		self.newGameText.setScale(0.35, 0.35)
		
		self.replayButton = viz.addButton()
		self.replayButton.setPosition(.05,0.88) #Set its position.
		self.replayButton.setScale(1,1) #Scale it.
		
		self.replayText = viz.addText("Open Replay", viz.SCREEN)
		self.replayText.setPosition(0.07,0.87)
		self.replayText.setScale(0.35, 0.35)
		
		self.offsetButton = viz.addButton()
		self.offsetButton.setPosition(.95,0.95) #Set its position.
		self.offsetButton.setScale(1,1) #Scale it.
		
		self.offsetText = viz.addText("Calibrate Offset", viz.SCREEN)
		self.offsetText.setPosition(0.74,0.94)
		self.offsetText.setScale(0.35, 0.35)
		
		self.sensitivityButton = viz.addButton()
		self.sensitivityButton.setPosition(.95,0.88) #Set its position.
		self.sensitivityButton.setScale(1,1) #Scale it.
		
		self.sensitivityText = viz.addText("Calibrate Sensitivity", viz.SCREEN)
		self.sensitivityText.setPosition(0.69,0.87)
		self.sensitivityText.setScale(0.35, 0.35)

		self.vizact.onbuttonup(self.newGameButton, self.onClickNewGame)
		self.vizact.onbuttonup(self.replayButton, self.onClickReplay)
		self.vizact.onbuttonup(self.offsetButton, self.onClickOffset)
		self.vizact.onbuttonup(self.sensitivityButton, self.onClickSensitivity)
		
	def onClickNewGame(self):
		self.game.permissions = 'w'
		self.game.outputFile = open("results/" + (str)(time.time()).replace(".", "") + ".txt", self.game.permissions)
		self.game.nPressed = 0
		self.game.start = time.time()
		self.game.accuracy = 0
		self.game.myActionStack.clearActions()
		self.game.simonActionStack.clearActions()
		self.game.simonActionStack.addRandomAction()
		self.game.showSequence()
		
	def onClickReplay(self):
		mydocs_pidl = shell.SHGetFolderLocation (0, shellcon.CSIDL_PERSONAL, 0, 0)
		pidl, display_name, image_list = shell.SHBrowseForFolder (
			win32gui.GetDesktopWindow (),
			mydocs_pidl,
			"Choose a File",
			shellcon.BIF_BROWSEINCLUDEFILES,
			None,
			None
			)
		
		if pidl != None:
			path = shell.SHGetPathFromIDList(pidl)
			self.game.permissions = 'r'
			self.game.outputFile = open(path, self.game.permissions)
			self.game.nPressed = 0
			self.game.start = time.time()
			self.game.accuracy = 0
			
	def onClickOffset(self):
		self.game.offset = self.game.getHandTracker().getPosition()
		if self.game.offset[0] == 0:
			self.game.offset = self.game.getSecondaryHandTracker().getPosition()
			
	def onClickSensitivity(self):
		self.game.range = self.game.handTracker.getPosition()
		self.game.scale = 60 / ( self.game.offset[0] - self.game.range[0] )